package com.sanjiahantang.youjiaclient.module.login.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.base.BaseBean;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.common.models.IUserModel;
import com.sanjiahantang.youjiaclient.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiaclient.module.login.views.ChangePasswordView;
import com.sanjiahantang.youjiaclient.module.login.views.impl.ChangePasswordActivity;
import com.sanjiahantang.youjiaclient.module.login.views.impl.LoginActivity;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.util.HashMap;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.RESPONSE_CODE_SUC;

/**
 * Created by zy on 2017/9/4.
 */

public class ChangePasswordPresenter extends BaseMvpPresenter<ChangePasswordView> implements CallBack<BaseBean> {

    private IUserModel userModel;
    public ChangePasswordPresenter(){
        userModel = new UserModel();
    }
    public void submit(Context context, String oldPass, String newPass) {
        Map<String,Object> params=new HashMap<>();
        params.put("token", ConfigValue.DATA_KEY);
        params.put("phone", SPUtils.get(context, SPConfig.PHONE,"").toString());
        params.put("pwd",oldPass);
        params.put("nowPwd",newPass);
        mView.showProgress();
        userModel.doChangePassword(context,params,new ChangePasswordCallBack());
    }

    public void modifyPayPwd(final Context context, String oldPass, String newPass, BaseView.OnLoadListener listener) {
        Map<String,Object> params=new HashMap<>();
        params.put("token", ConfigValue.DATA_KEY);
        params.put("phone", SPUtils.get(context, SPConfig.PHONE,"").toString());
        params.put("pwd",oldPass);
        params.put("nowPwd",newPass);
        if (mView == null) return;
        mView.showProgress();
        userModel.modifyPayPwd(context,params, new CallBack<StringBean>() {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response == null || response.getCode() != RESPONSE_CODE_SUC){
                    Utils.showToast(context,"修改失败");
                    return;
                }
                if (TextUtils.equals(response.getData(),"true")){
                    Utils.showToast(context, response.getMessage());
                    ((Activity)context).finish();
                }else{
                    Utils.showToast(context,"修改失败");
                }

            }


            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context, "修改失败");
            }
        });
    }

    private class ChangePasswordCallBack implements CallBack<StringBean>{
        @Override
        public void onSuccess(StringBean response, Context context,Object tag) {
            mView.hideProgress();
            if(response != null && response.getCode() == RESPONSE_CODE_SUC){

                if ("true".equals(response.getData())){
                    Utils.showToast(context, response.getMessage());
                    ((Activity)context).finish();
                }else{
                    Utils.showToast(context, "修改失败");
                }
            }
//            if (response.getCode().equals("1")) {aaaaaaaaaaaa
//                Map<String,String> params = new HashMap<>();
//                String key = response.getData().getKey();
//                SPUtils.put(context, SPConfig.KEY, key);
//                ConfigValue.DATA_KEY = key;
//                params.put("key", ConfigValue.DATA_KEY);
//                mView.showProgress();
//                userModel.loadUserInfo((BaseMvpActivity) context,params,LoginPresenter.this);
//            }

        }

        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }
    }
    @Override
    public void onSuccess(BaseBean response, Context context,Object tag) {
        mView.hideProgress();
        if (response.getCode() == RESPONSE_CODE_SUC)
        {
            LoginActivity.logout(context);
            ((BaseMvpActivity)context).finish();
        }
    }

    @Override
    public void onFailure(String message) {
        mView.hideProgress();
    }
}
